package xuhaijia.lanou3g.autohome.recommend.bulletin;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.custom.MyListener;
import xuhaijia.lanou3g.autohome.custom.PullToRefreshLayout;

/**
 * Created by dllo on 16/8/15.
 */
public class BulletinFragment extends BaseFragment {

    private ListView bulletinLv;
    private BulletinAdapter bulletinAdapter;
    int count = 30;
    private PullToRefreshLayout ptrlBulletin;

    @Override
    protected int setLayout() {
        return R.layout.fragment_bulletin;
    }

    @Override
    protected void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_bulletin_header , null);
        LinearLayout allBrand = (LinearLayout) view.findViewById(R.id.bulletin_allbrand);
        LinearLayout allLevel = (LinearLayout) view.findViewById(R.id.bulletin_alllevel);
        ptrlBulletin = bindView(R.id.ptrl_bulletin);
        ptrlBulletin.setOnRefreshListener(new MyListener(this));
        bulletinLv = bindView(R.id.bulletin_lv);
        bulletinLv.addHeaderView(view);
    }

    @Override
    protected void initData() {
        bulletinAdapter = new BulletinAdapter(mContext);
        mNetTool.getNetData(URLValues.BULLETIN_URL, BulletinBean.class, new NetTool.NetListener<BulletinBean>() {
            @Override
            public void onSuccess(BulletinBean bulletinBean) {
                bulletinAdapter.setBulletinBean(bulletinBean);
                bulletinLv.setAdapter(bulletinAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void load() {
// "http://app.api.autohome.com.cn/autov4.8.8/news/fastnewslist-pm1-b0-l0-s20-lastid0.json"
        String urlString = URLValues.BULLETIN_URL;
        int start = urlString.indexOf("http");
        int middle = urlString.indexOf("20-l");
        int end = urlString.indexOf("-last");
        count = count + 20;
        String urlStart = urlString.substring(start, middle);
        String urlEnd = urlString.substring(end , urlString.length());
        String urlFinal = urlStart + count + urlEnd;
        mNetTool.getNetData(urlFinal, BulletinBean.class, new NetTool.NetListener<BulletinBean>() {
            @Override
            public void onSuccess(BulletinBean bulletinBean) {
                bulletinAdapter.setBulletinBean(bulletinBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void refresh() {
        mNetTool.getNetData(URLValues.BULLETIN_URL, BulletinBean.class, new NetTool.NetListener<BulletinBean>() {
            @Override
            public void onSuccess(BulletinBean bulletinBean) {
                bulletinAdapter.setBulletinBean(bulletinBean);
            }
            @Override
            public void onError(String errorMsg) {

            }
        });
    }
}
